﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using System.Collections.Generic;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingInstitute : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IMasCountryBLL countryBLL = new MasCountryBLL();
        ITrainingInstituteBLL InstituteBLL = new TrainingInstituteBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                grdTRInstitute.DataSource = InstituteBLL.GetFull();
                grdTRInstitute.DataBind();
                LoadDDLCountrySeq();
            }
        }
        protected void grdTRInstitute_ItemCommand(object o, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                LoadDDLCountrySeq();
                UpdateUI("AddNew");

            }
            else if (e.CommandName == "Delete")
            {
                string TrainingInsSeq = grdTRInstitute.Items[e.Item.ItemIndex]["training_ins_seq"].Text;
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;

                TrainingInstituteInfo info = new TrainingInstituteInfo();
                info.training_ins_seq = Convert.ToInt32(TrainingInsSeq);
                info.updated_by = UserID;
                info.updated_when = dtNow;

                InstituteBLL.Delete(info);
            }
            else if (e.CommandName == "DataEdit")
            {
                UpdateUI("Update");
                string strTrainingInsDescr = grdTRInstitute.Items[e.Item.ItemIndex]["training_ins_descr"].Text;
                string strTrResponsible = grdTRInstitute.Items[e.Item.ItemIndex]["training_ins_responsible"].Text;
                string strTrainingInsSeq = grdTRInstitute.Items[e.Item.ItemIndex]["training_ins_seq"].Text;
                string strCountrySeq = grdTRInstitute.Items[e.Item.ItemIndex]["country_seq"].Text;

                txtTrainingInsSeq.Text = strTrainingInsSeq;
                txtTrainingInsDescr.Text = strTrainingInsDescr;
                ddlCountrySeq.SelectedValue = strCountrySeq;

            }
            grdTRInstitute.DataSource = InstituteBLL.GetFull();
            grdTRInstitute.DataBind();
            
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                string strTrainingInsDescr = txtTrainingInsDescr.Text;
                string strResponsible = txtTrainingInsRespons.Text;
                int strCountrySeq = Convert.ToInt32(ddlCountrySeq.SelectedValue);
                int UserID = ssMng.EmpSeqI;
                DateTime dtNow = DateTime.Now;

                UpdateUI("Save");

                TrainingInstituteInfo info = new TrainingInstituteInfo();
                info.training_ins_descr = strTrainingInsDescr;
                info.training_ins_responsible = strResponsible;
                info.country_seq = strCountrySeq;
                info.created_by = UserID;
                info.created_when = dtNow;

                InstituteBLL.Insert(info);

                grdTRInstitute.DataSource = InstituteBLL.GetFull();
                grdTRInstitute.DataBind();
            }
                     
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int TrainingInsSeq = Convert.ToInt32(txtTrainingInsSeq.Text);
            string strTrainingInsDescr = txtTrainingInsDescr.Text;
            string strResponsible = txtTrainingInsRespons.Text;
            int strCountrySeq = Convert.ToInt32(ddlCountrySeq.SelectedValue);
            int UserID = ssMng.EmpSeqI;
            DateTime dtNow = DateTime.Now;
            UpdateUI("Save");

            TrainingInstituteInfo info = new TrainingInstituteInfo();
            info.training_ins_seq = TrainingInsSeq;
            info.training_ins_descr = strTrainingInsDescr;
            info.training_ins_responsible = strResponsible;
            info.country_seq = strCountrySeq;            
            info.updated_by = UserID;
            info.updated_when = dtNow;
            InstituteBLL.Update(info);

            grdTRInstitute.DataSource = InstituteBLL.GetFull();
            grdTRInstitute.DataBind();
             
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
           ClearControl();
           UpdateUI("Cancel");
        }
        protected void ClearControl()
        {
            txtTrainingInsDescr.Text = "";
            txtTrainingInsRespons.Text = "";
            lblError.Text = "";
            ddlCountrySeq.SelectedValue = "";
            ddlCountrySeq.Text = "";
            
        }
        protected void LoadDDLCountrySeq()
        {
            IList<MasCountryInfo> infoList = countryBLL.ListMasCountries();
            ddlCountrySeq.DataSource = infoList;
            ddlCountrySeq.DataBind();
    
        }

        protected bool checkValidate()
        {
            bool bOK = true;
            string strTrainingInsDescr = (txtTrainingInsDescr.Text).ToString();
            string strCountrySeq = ddlCountrySeq.SelectedValue;
            if (strTrainingInsDescr.Length == 0)
            {
                bOK = false;
                lblError.Text = "* กรุณาใส่ชื่อหน่วยงาน";
            }

            else if (strCountrySeq == "")
            {
                bOK = false;
                lblError.Text = "* กรุณาเลือกประเทศ";
            }
            return bOK;
        }
        
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
   
                case "AddNew":                
                    InstituteEdit.Visible = true;
                    btnSave.Visible = true;
                    btnUpdate.Visible = false;
                    tblTRInstitute.Visible = false;
                      
                    break;

                case "Save":
                    InstituteEdit.Visible = false;
                    tblTRInstitute.Visible = true;
                    lblError.Text = "";

                    break;

                case "Cancel":
                    tblTRInstitute.Visible = true;
                    btnUpdate.Visible = false;
                    InstituteEdit.Visible = false;
                    
                    break;

                case "Update":
                    InstituteEdit.Visible = true;
                    btnUpdate.Visible = true;
                    btnSave.Visible = false;
                    tblTRInstitute.Visible = false;

                    break;

            }
       }
        
    }
}
